package com.allinpay.merchantweb.service.impl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.allinpay.merchantweb.common.exception.ViewException;
import com.allinpay.merchantweb.constants.ReturnCodeConstants;
import com.allinpay.merchantweb.model.Manager;
import com.allinpay.merchantweb.request.CommonReqBean;
import com.allinpay.merchantweb.request.ManagerRequest;
import com.allinpay.merchantweb.response.AppCommonResponse;
import com.allinpay.merchantweb.service.LoginService;
import com.google.gson.reflect.TypeToken;

@Service
public class LoginServiceImpl implements LoginService {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
	
	@Autowired
	RestRequestService restRequestService;
    /**
     * 功能：登录服务
     *
     */
	@Override
	public AppCommonResponse<Manager> login(CommonReqBean<ManagerRequest> reqBean) throws ViewException {
		//定义返回对象
		AppCommonResponse<Manager> respBean=new AppCommonResponse<Manager>();
		try {
	        logger.info("reqBean:"+reqBean.toString());
	        //调用MerchantApp接口
		    respBean = restRequestService.postToAppForSingleByObj("/adminLogin", reqBean,new TypeToken<AppCommonResponse<Manager>>() {}.getType());
			if( respBean != null) {
		        String subCode = respBean.getSubCode();
		        String subMsg = respBean.getSubMsg();
		        if(!ReturnCodeConstants.SUB_CODE_SUCC.equals(subCode)){
		        	logger.error("login error:"+subCode+"-"+subMsg );
		        	ViewException view = new ViewException();
		        	view.setErrorCode(subCode);
		        	view.setErrorMsg(subMsg);
		        	throw view;
		        }
			}else {
	        	ViewException view = new ViewException();
	        	view.setErrorCode(ReturnCodeConstants.CONN_RETURN_ERROR);
	        	view.setErrorMsg(ReturnCodeConstants.CONN_RETURN_MSG);
	        	throw view;
			}
	        
		}catch(ViewException e) {
			logger.error("用户登录报错："+e.getErrorCode()+e.getErrorMsg());
			throw e;
		}catch(Exception e) {
			logger.error("用户登录报错:"+e);
		}
		return respBean;
	}

}
